<template>
  <div>
    <!-- 新增修改表单 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="DialogFormVisibleCom" :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="100px"
               style="width: 90%; margin:10px auto;">
        <vxe-table
          border
          :data="tableData"
          :edit-config="{trigger: 'click', mode: 'cell'}"
          @checkbox-change="checkboxChangeEvent"
          @checkbox-all="checkboxChangeEvent"
          @edit-actived="editActivedEvent">
          <vxe-table-column type="checkbox" width="60"></vxe-table-column>
          <!-- <vxe-table-column type="seq" width="60"></vxe-table-column> -->
          <vxe-table-column field="term_name" title="合集"
                            :edit-render="{name: 'input', attrs: {disabled: true}}"></vxe-table-column>
          <vxe-table-column field="a_val" :title="'a曲线-'+ parentData.a_cv_name"
                            :edit-render="{name: '$input',  props: {type: 'number', disabled: aGather}}"></vxe-table-column>
          <vxe-table-column field="check" title="运算符"
                            :edit-render="{name: 'select', options: operator}"></vxe-table-column>
          <vxe-table-column field="b_val" :title="'b曲线-'+ parentData.b_cv_name"
                            :edit-render="{name: '$input',  props: {type: 'number', disabled: bGather}}"></vxe-table-column>
        </vxe-table>


      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <!-- <el-button @click="resetFormData">
          重置
        </el-button> -->
        <el-button type="primary" @click="createData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
// const calendarTypeOptions = [
//   { key: 'lp_code', display_name: 'lp_name' },
// ]
import {findoperator} from '../service/baseApi'
import resetFormSubmitData from '@/views/mixins/resetFormSubmitData'

export default {
  name: 'submitForm',
  mixins: [resetFormSubmitData],
  components: {
    //PageHeaderLayout,
  },
  props: {
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    dialogStatus: {
      type: String,
    },
    formSubmitData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
          term_name: '',
          a_term_weight: '',
          b_term_weight: '',
          remark: '',
        }
      }
    },
    parentData: {
      type: Object,
      default: function () {
        return {
          id: undefined,
        }
      }
    },
    setList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },

  // 定义属性
  data() {
    return {
      textMap: {
        update: '修改',
        create: '新增'
      },
      rules: {
        gs_cs_code: [{required: true, message: '生成方案编号必填', trigger: 'change'}],
        gs_cs_name: [{required: true, message: '生成方案名称必填', trigger: 'change'}],
      },
      tablePage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      operator: [],//运算符下拉
      dataList: [],
      // a\b是否禁用
      aGather: false,
      bGather: false,
      // 勾选的数据
      checkedData: []

    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom: {
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom: {
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    },
    tableData: {
      get() {
        return this.setList
      },
      set(val) {
        return this.setList
      }
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    // 获取移动平均曲线下拉数据
    getlist() {
      // 获取运算符下拉框下拉
      findoperator().then(({data}) => {
        data.rows.map(item => {
          this.operator.push({
            label: item.name,
            value: item.code,
            disabled: false
          })
        })
      })
    },
    // 重置
    resetFormData() {
      let exclude = []
      if (this.dialogStatus === 'create') {
        exclude = [
          'gs_cs_code',
        ]
      }
      if (this.dialogStatus === 'update') {
        exclude = [
          'id',
          'gs_cs_code'
        ]
      }
      // this.$emit('resetFormDataEmit', {exclude})
    },
    // 新增
    createData() {
      let obj = {}
      this.$set(obj, 'm_id', this.parentData.id)
      if (this.checkedData.length) {
        let dataList = this.checkedData
        for (let i in dataList) {
          if (dataList[i].a_term_name == true && dataList[i].a_val == '') {
            this.$message({
              message: dataList[i].term_name + '的a曲线不能为空',
              type: 'warning'
            });
            break;
          } else if (dataList[i].check == '') {
            this.$message({
              message: dataList[i].term_name + '的运算符不能为空',
              type: 'warning'
            });
            break;
          } else if (dataList[i].b_term_name == true && dataList[i].b_val == '') {
            this.$message({
              message: dataList[i].term_name + '的b曲线不能为空',
              type: 'warning'
            });
            break;
          } else {
            this.$set(obj, dataList[i].term_name, ((dataList[i].a_val != '' ? dataList[i].a_val : 0) + '_' + dataList[i].check + '_' + (dataList[i].b_val != '' ? dataList[i].b_val : 0)))
            if (Number(i) == Number(dataList.length - 1)) {
              this.$emit('handleFromCreateDataEmit', obj)
            }
          }
        }
      } else {
        this.$message({
          message: '请最少勾选一项！',
          type: 'warning'
        });
      }
    },
    // 单元格编辑触发的方法
    editActivedEvent({rowIndex, row}) {
      console.log(row)
      this.aGather = !row.a_term_name
      this.bGather = !row.b_term_name
    },
    checkboxChangeEvent(row) {
      console.log(row.selection)
      this.checkedData = row.selection
    },

  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getlist()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  },
  beforeCreate() {
  }, // 生命周期 - 创建之前
  beforeMount() {
  }, // 生命周期 - 挂载之前
  beforeUpdate() {
  }, // 生命周期 - 更新之前
  updated() {
  }, // 生命周期 - 更新之后
  beforeDestroy() {
  }, // 生命周期 - 销毁之前
  destroyed() {
  }, // 生命周期 - 销毁完成
  activated() {
  }, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>

</style>
